﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Catalyst.Mvc.Extensions
{
	public static class IEnumerable
	{
		public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, string> text, Func<T, string> value)
		{
			return ToSelectList(source, text, value, x => false);
		}

		public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, string> text, Func<T, string> value, Func<T, bool> selected)
		{
			foreach (T item in source)
			{
				yield return new SelectListItem
				{
					Text = text(item),
					Value = value(item)
				};
			}

			yield break;
		}
	}
}
